using System;
using System.Collections.Generic;

namespace XTengine
{
    public class ResourcePool<T> where T : class, new()
    {
        private Queue<T> queue;
        private Action<T> initDel;

        public int Count { get { return this.queue.Count; } }

        public ResourcePool(Action<T> initializer)
        {
            this.queue = new Queue<T>();

            this.initDel = initializer;
        }

        public T New()
        {
            T item;

            if (queue.Count > 0)
                item = queue.Dequeue();
            else
                item = new T();

 
            if (initDel != null)
                initDel(item);

            return item;
        }

        public void Free(T item)
        {
            queue.Enqueue(item);
        }
    }
}